package com.credit.jumu.playcard.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.credit.jumu.playcard.BuildConfig;
import com.credit.jumu.playcard.R;
import com.credit.jumu.playcard.cardapp.CardApp;
import com.credit.jumu.playcard.utils.DataSharedPreference;
import com.credit.jumu.playcard.utils.JsonUtils;
import com.credit.jumu.playcard.utils.LogUtils;
import com.credit.jumu.playcard.utils.MD5Encoder;
import com.credit.jumu.playcard.utils.SignUtil;
import com.credit.jumu.playcard.utils.SystemUtil;
import com.credit.jumu.playcard.utils.ToastUtil;
import com.credit.jumu.playcard.utils.TypeUtils;
import com.credit.jumu.playcard.utils.UrlsUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import net.sf.json.JSONObject;

import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2017/10/21/021.
 */
//忘记密码
public class LoginPassSetActivity extends Activity implements View.OnClickListener {
	EditText setpassnum, smsCodenum, mypass;
	TextView getcode, toResetPass;
	private DataSharedPreference ds;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loginpassset);
		setpassnum = findViewById(R.id.setpassnum);
		smsCodenum = findViewById(R.id.smsCodenum);
		mypass = findViewById(R.id.mypass);
		getcode = findViewById(R.id.getcode);
		toResetPass = findViewById(R.id.toResetPass);
		getcode.setOnClickListener(this);
		toResetPass.setOnClickListener(this);
		ds = new DataSharedPreference(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.getcode:
				if (TextUtils.isEmpty(setpassnum.getText().toString())) {
					ToastUtil.showShortToast("号码不能为空");
				} else {
					getSmsCode(setpassnum.getText().toString());
				}
				break;
			case R.id.toResetPass:
				//EditText setpassnum, smsCodenum, mypass;
				if (TextUtils.isEmpty(setpassnum.getText().toString())) {
					ToastUtil.showShortToast("号码不能为空");
				} else if (TextUtils.isEmpty(smsCodenum.getText().toString())) {
					ToastUtil.showShortToast("验证码不能为空");
				} else if (TextUtils.isEmpty(mypass.getText().toString())) {
					ToastUtil.showShortToast("密码不能为空");
				} else {
					toReset(setpassnum.getText().toString(), smsCodenum.getText().toString(), mypass.getText().toString());
				}
				break;
		}

	}

	public void getSmsCode(String num) {
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("mobile", num);
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("mobile", num);
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("source", CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);
		OkGo.<String>post(UrlsUtils.SEND_CODE)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								if (!TextUtils.isEmpty(response.body())) {
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									String code = TypeUtils.getJsonString(responseJson, "code", "");
									String message = TypeUtils.getJsonString(responseJson, "message", "");
									ToastUtil.showShortToast(message);
								}
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyionGetMessageCodeerr------" + response.body() + response.message() + response.code());
							}
						}
				);
	}

	public void toReset(String phone, String code, String pwd) {
		LinkedHashMap<String, String> mapSign = new LinkedHashMap<>();
		mapSign.put("actionType", "forgetpwd");
		mapSign.put("deviceCode", ds.getUUID());
		mapSign.put("intefaceType", CardApp.intefaceType);
		mapSign.put("mobile", phone);
		mapSign.put("passWord", MD5Encoder.encodeBase64(pwd));
		mapSign.put("smsCode", code);
		mapSign.put("source", CardApp.source);
		mapSign.put("terminalId", "A");
		mapSign.put("timeStamp", SystemUtil.getSystemTime());
		mapSign.put("version", BuildConfig.VERSION_NAME);
		mapSign.put("key", CardApp.key);

		LinkedHashMap<String, String> mapPramas = new LinkedHashMap<>();
		mapPramas.put("accountId", "");
		mapPramas.put("actionType", "forgetpwd");
		mapPramas.put("deviceCode", ds.getUUID());
		mapPramas.put("intefaceType", CardApp.intefaceType);
		mapPramas.put("mobile", phone);
		mapPramas.put("passWord", MD5Encoder.encodeBase64(pwd));
		mapPramas.put("sign", MD5Encoder.encode(SignUtil.getSign(mapSign)));
		mapPramas.put("smsCode", code);
		mapPramas.put("source",CardApp.source);
		mapPramas.put("terminalId", "A");
		mapPramas.put("timeStamp", SystemUtil.getSystemTime());
		mapPramas.put("token", "");
		mapPramas.put("version", BuildConfig.VERSION_NAME);

		OkGo.<String>post(UrlsUtils.FORGET_PSWD)
				.params(mapPramas)
				.execute(
						new StringCallback() {
							@Override
							public void onSuccess(Response<String> response) {
								if (!TextUtils.isEmpty(response.body())) {
									LogUtils.println("zhangyiyi------LoginPassSet" + response.body());
									JSONObject responseJson = JsonUtils.getJsonObject(response.body().toString());
									String code = TypeUtils.getJsonString(responseJson, "code", "");
									String message = TypeUtils.getJsonString(responseJson, "message", "");
									ToastUtil.showShortToast(message);
									if ("9999".equals(code)) {
										LoginPassSetActivity.this.finish();
									}
								}
							}

							@Override
							public void onError(Response<String> response) {
								LogUtils.println("zhangyiyiLoginPassSeterr------" + response.body() + response.message() + response.code());
							}
						}
				);

	}

}
